#include "tree_linklist.h"

int Tree_insertnode(TreeNode_t ** root,int data){
    //如果该节点是为空，表明该节点为该子树的根节点，反之继续往下寻找
    if((*root) == NULL){
        LOG("root create\n");
        TreeNode_t *node = (TreeNode_t *)malloc(sizeof(TreeNode_t));
        if (node == NULL)
        {
            LOG("node create failed\n");
            return -1;
        }
        node->data = data;
        node->left = NULL;
        node->right = NULL;
        (*root) = node;
        return 0;
    }
    //如果该节点的数据要比当前节点要大，就要放到该节点的左子树，反之就是放入右子树
    if(data > (*root)->data){
        LOG("left\n");
        Tree_insertnode(&(*root)->left,data);
    }
    if(data < (*root)->data){
        LOG("right\n");
        Tree_insertnode(&(*root)->right,data);
    }
    return 0;
}


int Tree_fontselect(TreeNode_t * root){
    if(root!=NULL){
        printf("%d,",root->data);
        Tree_fontselect(root->left);
        Tree_fontselect(root->right);
    }
    return 0;
}

int Tree_midselect(TreeNode_t * root){
    if(root!=NULL){
        Tree_midselect(root->left);
        printf("%d,",root->data);
        Tree_midselect(root->right);
    }
    return 0;
}

int Tree_backselect(TreeNode_t * root){
    if(root!=NULL){
        Tree_backselect(root->left);
        Tree_backselect(root->right);
        printf("%d,",root->data);
    }
    return 0;
}